/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import pl.milib.data.info.MIEventInfo;
import pl.milib.data.info.ScrollInfo;
import pl.milib.managers.EnterFrameBroadcaster;
import pl.milib.managers.EnterFrameReciver;
import pl.milib.mc.MIScrollable;
import pl.milib.mc.service.MIMC;
import pl.milib.util.MILibUtil;
import pl.milib.util.MIMCUtil;

/**
 * @author Marek Brun
 */
class pl.milib.mc.ScrolledMC extends MIScrollable implements EnterFrameReciver {
	
	public var mimc : MIMC;
	private var maskMC : MovieClip;
	private var scrollUnit : Number;
	private var maskMCLength : Number;
	private var mcLength : Number;
	private var lengthName : String;	private var axis : String;
	
	public function ScrolledMC(mc:MovieClip, maskMC:MovieClip, $scrollUnit:Number, $isXAxis:Boolean) {
		mimc=MIMC.forInstance(mc);
		mimc.addListener(this);
		this.maskMC=maskMC;
		scrollUnit=$scrollUnit==null ? 100 : $scrollUnit;
		EnterFrameBroadcaster.start(this);
		addDeleteWith(mimc);
		mimc.mc.setScroll=MILibUtil.createDelegate(this, setScrollN01);
		if($isXAxis){
			lengthName='width';			axis='x';
		}else{
			lengthName='height';
			axis='y';
		}
	}//<>
	
	private function getScrollN01(Void):Number {
		var distY:Number=maskMC['_'+axis]-mimc.mc['_'+axis];
		var scroll01:Number=distY/(getMCLength()-maskMC['_'+lengthName]);
		return scroll01;
	}//<<
	
	public function getScrollData(Void):ScrollInfo {
		return (new ScrollInfo(getMCLength(), maskMC['_'+lengthName], getScrollN01()));
	}//<<
	
	public function setScrollN01(n01:Number):Void {
		if(!getIsScrollable()){
			mimc.mc['_'+axis]=maskMC['_'+axis];
			return;
		}
		if(n01<0){ n01=0; }else if(n01>1){ n01=1; }
		mimc.mc['_'+axis]=maskMC['_'+axis]-n01*(getMCLength()-maskMC['_'+lengthName]);
		MIMCUtil.snapMCPosToPixels(mimc.mc);
		bev(event_NewScrollData);
	}//<<
	
	public function getMCLength(Void):Number {
		if(isNaN(mcLength)){
			return mimc.mc['_'+lengthName]+6;
		}else{
			return mcLength;
		}
	}//<<
	
	public function setMCLength(mcLength:Number):Void {
		this.mcLength=mcLength;
		setScrollN01(getScrollN01());
	}//<<
	
	public function getMaskLength(Void):Number {
		return maskMC['_'+lengthName];
	}//<<
	
	public function setScrollByOneUnit(turn:Number):Void {
		if(turn<0){
			setScrollN01(getScrollN01()-scrollUnit/(getMCLength()-maskMC['_'+lengthName]));
		}else{
			setScrollN01(getScrollN01()+scrollUnit/(getMCLength()-maskMC['_'+lengthName]));
		}
	}//<<
	
	public function getIsScrollable(Void):Boolean {
		return getMCLength()>maskMC['_'+lengthName];
	}//<<
	
//****************************************************************************
// EVENTS for ScrolledMC
//****************************************************************************
	private function onEvent(ev:MIEventInfo) {
		//super.onEvent(ev);
		switch(ev.hero){
			case mimc:
				switch(ev.event){
					case mimc.event_EnterNewFrame:
						setScrollN01(0);
					break;
				}
			break;
		}
	}//<<Events
	
	public function onEnterFrame(id):Void {
		if(isNaN(mcLength)){
			if(maskMCLength!=maskMC['_'+lengthName] || mcLength!=getMCLength()){
				maskMCLength=maskMC['_'+lengthName];
				bev(event_NewScrollData);
			}
		}
	}//<<
	
}